package com.supermap.dialogs;

import com.supermap.consts.AttrConsts;
import com.supermap.data.*;
import com.supermap.dialogs.advance.CJDialog;
import com.supermap.mapping.ThemeLabel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ThemeLabelDialog extends CJDialog {
    private JComboBox<String> valueComboBox;

    private DatasetVector datasetVector;
    private ThemeLabel themeLabel;
    private Color labelColor;
    private JoinItems joinItems;

    public ThemeLabelDialog(Frame owner, String title, boolean modal, DatasetVector datasetVector, JoinItems joinItems) {
        super(owner, title, modal, 260, 115, new Object[]{datasetVector, joinItems});
    }

    public ThemeLabel getThemeLabel() {
        return themeLabel;
    }

    @Override
    protected void initializeArgs(Object[] args) {
        this.datasetVector = (DatasetVector) args[0];
        this.joinItems = (JoinItems) args[1];
    }

    @Override
    protected void initializeCore() {
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                super.windowClosed(e);
                themeLabel = null;
            }
        });
        super.confirmBtn.setText("创建");

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(2, 1));
        add(mainPanel, BorderLayout.CENTER);

        // 下拉框
        valueComboBox = new JComboBox<>();
        FieldInfos fieldInfos = datasetVector.getFieldInfos();
        for (int i = 0; i < fieldInfos.getCount(); i++) {
            FieldInfo fieldInfo = fieldInfos.get(i);
            FieldType fieldType = fieldInfo.getType();
            if (fieldType != FieldType.LONGBINARY && fieldType != FieldType.JSONB) {
                valueComboBox.addItem(fieldInfo.getName());
            }
        }
        if (joinItems != null && joinItems.getCount() > 0) {
            for (int i = 0; i < joinItems.getCount(); i++) {
                JoinItem joinItem = joinItems.get(i);
                String foreignTableName = joinItem.getForeignTable();
                DatasetVector foreignDatasetVector = (DatasetVector) datasetVector.getDatasource().getDatasets().get(foreignTableName);
                FieldInfos foreignFieldInfos = foreignDatasetVector.getFieldInfos();
                for (int j = 0; j < foreignFieldInfos.getCount(); j++) {
                    FieldInfo fieldInfo = foreignFieldInfos.get(j);
                    FieldType fieldType = fieldInfo.getType();
                    if (fieldType != FieldType.LONGBINARY && fieldType != FieldType.JSONB) {
                        valueComboBox.addItem(foreignDatasetVector.getTableName() + AttrConsts.POINT + fieldInfo.getName());
                    }
                }
            }
        }
        mainPanel.add(valueComboBox);

        // 标签颜色设置
        labelColor = Color.BLACK;
        JButton labelColorButton = new JButton();
        labelColorButton.setText("标签颜色");
        labelColorButton.setBackground(labelColor);
        labelColorButton.addActionListener(e -> {
            labelColor = JColorChooser.showDialog(this, "标签颜色", labelColor);
            labelColorButton.setBackground(labelColor);
        });
        mainPanel.add(labelColorButton);
    }

    @Override
    protected void doConfirmCore() {
        setVisible(false);
        String labelExpression = (String) valueComboBox.getSelectedItem();
        themeLabel = new ThemeLabel();
        themeLabel.setLabelExpression(labelExpression);
        TextStyle textStyle = new TextStyle();
        textStyle.setAlignment(TextAlignment.MIDDLECENTER);
        textStyle.setForeColor(labelColor);
        textStyle.setFontName("宋体");
        themeLabel.setUniformStyle(textStyle);
    }
}
